package org.jimmy.tang.spring.transaction;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

/**
 * @author jimmy
 * 
 */
public class BankServiceImpl implements BankService {

    private UserDao bankDao;
    private TransactionDefinition txDefinition;
    private PlatformTransactionManager txManager;

    /* (non-Javadoc)
     * @see org.jimmy.tang.spring.transaction.BankService#transfer(long, long, double)
     */
    @Override
    public boolean transfer(long fromId, long toId, double amount) {
        TransactionStatus txStatus = txManager.getTransaction(txDefinition);
        boolean result = false;
        try {
            result = bankDao.transfer(fromId, toId, amount);
            txManager.commit(txStatus);
        } catch (Exception e) {
            result = false;
            txManager.rollback(txStatus);
            System.out.println("Transfer Error!");
        }
        return result;
    }

}
